package com.prig.base.service;

import cn.hutool.core.util.StrUtil;
import com.prig.base.DTO.input.PageKeywordSortDto;
import com.prig.base.entity.Permission;
import com.prig.base.repository.PermissionRepository;
import com.prig.base.util.ChangeType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.List;

@Service
public class PermissionService {
    private PermissionRepository permissionRepository;
    @Autowired
    public void setPermissionRepository(PermissionRepository permissionRepository) {
        this.permissionRepository = permissionRepository;
    }

    public Permission savePermission(Permission permission){
        return permissionRepository.save(permission);
    }

    public List<Permission> savePermission(List<Permission> permissions){
        return permissionRepository.saveAll(permissions);
    }

    public List<Permission> getPermissions(){
        return permissionRepository.findAll();
    }
    public List<Permission> getPermissionsIsViewIsTrue(){
        return permissionRepository.findByIsViewIsTrue();
    }

    public Page<Permission> getPermissions(PageKeywordSortDto dto){
        List<Sort.Order> orders = new ArrayList<>();
        orders.add(new Sort.Order(Sort.Direction.ASC,"parentId"));
        orders.add(new Sort.Order(Sort.Direction.ASC,"parentIds"));
        orders.add(new Sort.Order(Sort.Direction.ASC,"sortRank"));
        Pageable pageable = PageRequest.of(dto.getSQLPage(),dto.getLimit(),Sort.by(orders));
        if (StrUtil.isNotBlank(dto.getKeyword())){
            dto.setKeyword("%"+dto.getKeyword()+"%");
            return permissionRepository.findByNameLike(dto.getKeyword(),pageable);
        }
        return permissionRepository.findAll(pageable);
    }

    public List<Permission> getPermissions(String roleIds){
        return permissionRepository.findByRoleIds(ChangeType.String2Long(roleIds));
    }

    public void delete(@NotBlank String ids) {
        permissionRepository.deleteByIds(ChangeType.String2Long(ids));
    }

    public Permission getPermission(long permissionId) {
        return permissionRepository.getOne(permissionId);
    }

    public List<Permission> getMenu(long adminId) {
        List<Permission> permissions;
        if (adminId==0L){
            permissions = permissionRepository.findByIsViewIsTrue();
        }else {
            permissions = permissionRepository.getMenu(adminId);
        }
        permissions = setChildren(permissions);
        return permissions;
    }

    public List<Permission> setChildren(List<Permission> permissions) {
        List<Permission> result = new ArrayList<>();
        //儿子找爸爸
        for (int i=0;i<permissions.size();i++){
            //儿子
            if(permissions.get(i).getParentId()==0){
                result.add(permissions.get(i));
                continue;
            }
            for (Permission permission:permissions) {
                //baba
                if(permission.getId() == permissions.get(i).getParentId()){
                    if(permission.children == null){
                        permission.children = new ArrayList<>();
                    }
                    permission.children.add(permissions.get(i));
                    break;
                }
            }
        }
        return result;
    }

}
